package pl.edu.agh.mobsys.utils;

import java.util.ArrayList;

import pl.edu.agh.mobsys.R;
import pl.edu.agh.mobsys.soap.JobData;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SmoaJobsArrayAdapter extends BaseAdapter {

	private final Context context;
	private ArrayList<JobData> jobs;

	public SmoaJobsArrayAdapter(Context context, ArrayList<JobData> jobs) {
		this.context = context;
		this.jobs = jobs;
	}

	public int getCount() {
		return jobs.size();
	}

	public Object getItem(int position) {
		return jobs.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		JobData sJob = this.jobs.get(position);
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.contacts_row, null);
		}
		try {
			String rowText = "Name: " + sJob.getName() + "\n" + "Command: "
					+ sJob.getCommand();
			if ("".equals(sJob.getArgs()) == false) {
				rowText += "\nArguments: " + sJob.getArgs();
			}
			rowText += "\nState: " + sJob.getJobState().name().toLowerCase();
			((TextView) v.findViewById(R.id.text)).setText(rowText);
			int resId = 0;
			if (sJob.getJobState() == JobData.JobState.QUEUED) {
				resId = R.drawable.new_sprocket;
			} else if (sJob.getJobState() == JobData.JobState.EXECUTING) {
				resId = R.drawable.running_sprocket;
			} else if (sJob.getJobState() == JobData.JobState.PAUSED) {
				resId = R.drawable.new_sprocket;
			} else if (sJob.getJobState() == JobData.JobState.FINISHED) {
				resId = R.drawable.finnished_sprocket;
			} else if (sJob.getJobState() == JobData.JobState.CANCELLED) {
				resId = R.drawable.cancelled_sprocket;
			} 
			((ImageView) v.findViewById(R.id.image)).setImageResource(resId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}

}
